﻿package src {		
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import flash.display.MovieClip;	
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;	
	
	import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.*; 
	//////my classes
	import src.api.Request;
	import src.api.Actions;
	import src.components.Menu;
	import src.components.MenuButton;	
	import src.entity.Scheme;
	import src.common.OwnerEvent;
	// astra
	import com.yahoo.astra.fl.managers.AlertManager;
	
	public class Main extends MovieClip {
		
		private var request:Request
		private var menuButtons:Array;
		private var menu:Menu;
		private var currentScheme:Scheme;		
		private var config:Array = new Array( {text:'Схемы', img:'Schema',handler:listShems},
											  {text:'Элемент', img:'ElementImg', handler:createNewElement },
											  {text:'Узел', img: 'NodeImg', handler:createNewNode },
											  {text:'Связь', img:'LinkImg', handler:createNewLink}
									);
		private var ownerEvent:OwnerEvent;
		/*
		* 	create buttons for menu by config
		*/ 
		private function createMenuButtons() {
			this.menuButtons = new Array();			
			var i:Number;
			for(i=0; i < this.config.length; i++){
				var but1 = new MenuButton();
				but1.setTooltip(this.config[i].text);				
				var ClassReference:Class = getDefinitionByName(this.config[i].img) as Class;
				var img = new ClassReference();	
				but1.setImg(img);				
				if (this.config[i].handler && this.config[i].handler != null && this.config[i].handler!=''){
					but1.addEventListener(MouseEvent.CLICK, this.config[i].handler);
				}
				this.menuButtons[i] = but1;
			}			
		}
		
		/*
		* 	constructor
		*/ 
		public function Main(){			
			this.createMenuButtons();
			this.menu = new Menu(this.menuButtons);					
			this.menu.x = 0;
			this.menu.y = 0;
			addChild(this.menu);
			this.ownerEvent = OwnerEvent.getInstance();
			// in this case need change owner of event
			this.addEventListener(MouseEvent.MOUSE_UP, globalOnUp);
			this.addEventListener(MouseEvent.CLICK, globalOnUp);
			this.addEventListener(MouseEvent.DOUBLE_CLICK, globalOnUp);
		//	request = new Request();
		//	request.send(Actions.SAVE_CIRCUIT);
			this.stage.stageFocusRect = null;// To remove the crappy yellow rect on all focused InteractiveObject
		}	
		
		
		/*
		*  The method listens to all "onUp" events and reset ovner event
		*/  
		private function globalOnUp(event:MouseEvent) {			
			this.ownerEvent.notifyAboutResetEvent();// resetOwnerEvent();
		}
		
		
		/*
		* 	Listeners for menu button
		*/
		public function listShems(e:MouseEvent) {
			if(this.ownerEvent.getOwnerEvent() == null){
				this.currentScheme = null ///!!!!!!
				this.currentScheme = new Scheme(this.stage);
				this.currentScheme.x = 0;
				this.currentScheme.y = 0;			
				addChildAt(this.currentScheme, this.numChildren - 1);			
			}	
		}
		//		
		public function createNewElement(e:MouseEvent) {
			if (this.currentScheme) {				
				this.currentScheme.addElement();					
			}else {
				AlertManager.createAlert(this, "Необходимо создать схему", "Ошибка"); 			
			}
		}
		
		public function createNewNode(e:MouseEvent) {
			if (this.currentScheme) {				
				this.currentScheme.addNode();					
			}else {
				AlertManager.createAlert(this, "Необходимо создать схему", "Ошибка"); 			
			}			
		}
		
		public function createNewLink(e:MouseEvent) {
			if (this.currentScheme) {				
				this.currentScheme.addLink();					
			}else {
				AlertManager.createAlert(this, "Необходимо создать схему", "Ошибка"); 			
			}			
		}
		/////////////////
	}
	
}